package com.exam.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exam.entity.ApiResult;
import com.exam.entity.Message;
import com.exam.serviceimpl.MessageServiceImpl;
import com.exam.util.ApiResultHandler;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = {"/messagecontroller"})
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    @RequestMapping("/messages/{page}/{size}")
    public ResponseEntity<Page<Message>> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Message> messagePage = new Page<>(page,size);
        List<Message> messageList=this.messageService.findAll(page,size);
        System.out.println(messageList);
       return new ResponseEntity<Page<Message>>((Page<Message>) messageList, HttpStatus.OK);
    }

    @RequestMapping("/findByIdmessage/{id}")
    public ResponseEntity<Message> findById(@PathVariable("id") Integer id) {
        Message res = messageService.findById(id);
        return new ResponseEntity<Message>(res,HttpStatus.OK);
    }

    @RequestMapping("/deletemessage/{id}")
    public ResponseEntity<Integer> delete(@PathVariable("id") Integer id) {
        int res = messageService.delete(id);
        return new ResponseEntity<Integer>(res,HttpStatus.OK);
    }

    @RequestMapping("/message")
    public ResponseEntity<Integer> add(@RequestBody Message message) {
        int res = messageService.add(message);
       return new ResponseEntity<Integer>(res,HttpStatus.OK);
    }
}
